package one.bugu.android.demon.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.CoinBean;
import one.bugu.android.demon.bean.MapBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseFragment;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.activity.SetWarnActivity;
import one.bugu.android.demon.ui.adapter.CoinAdapter;
import one.bugu.android.demon.ui.adapter.PlatfromAdapter;
import one.bugu.android.demon.ui.dialog.DialogHint;
import one.bugu.android.demon.ui.dialog.DialogDelHint;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.ParamUtils;
import one.bugu.android.demon.util.PreferencesUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 添加币种页面
 */
@LKContentView(R.layout.fragment_addcoin)
public class CoinFragment extends MyBaseFragment {

    /**
     * 展示平台列表
     */
    @LKInjectView(R.id.listview_platform)
    private ListView platformListView;


    private PlatfromAdapter platfromAdapter;
    private CoinAdapter coinAdapter;

    /**
     * 侧滑界面
     */
    @LKInjectView(R.id.drawer_layout)
    private DrawerLayout drawerLayout;
    @LKInjectView(R.id.menu_layout_right)
    private RelativeLayout coinLayout;

    /**
     * 展示平台币的列表
     */
    @LKInjectView(R.id.listview_coin)
    private ListView coinListView;

    /**
     * 添加弹框
     */
    private DialogHint dialogHint;

    /**
     * 移除弹框
     */
    private DialogDelHint dialog_delHint;

    /**
     * 平台币rowId
     */
    private long rowId;

    /**
     * 添加移除币的图片
     */
    @LKInjectView(R.id.add_icon)
    private ImageView addIcon;

    /**
     * 币的平台
     */
    @LKInjectView(R.id.add_platform)
    private TextView add_platform;

    private String token;

    /**
     * 新老用户标识
     */
    private boolean isNew;

    @Override
    protected void initView() {
        super.initView();

        /**
         * listview底部添加空白
         */
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_footer, null);
        platformListView.addFooterView(view);

        platfromAdapter = new PlatfromAdapter(getActivity());
        platformListView.setAdapter(platfromAdapter);
        coinAdapter = new CoinAdapter(getActivity());
        coinListView.setAdapter(coinAdapter);

        /**
         * 添加弹框文字设置
         */
        dialogHint = new DialogHint(getActivity(),"自选成功！",true);
        dialogHint.setCancleText("继续自选");
        dialogHint.setOkText("前往设置");
        dialogHint.setCancelBtnTextColor("#5856D0");

        /**
         * 移除弹框文字
         */
        dialog_delHint = new DialogDelHint(getActivity(), "删除后您设置的数据将全部清空", true);
        dialog_delHint.setCancleText("我再想想");
        dialog_delHint.setOkText("确定删除");
    }

    /**
     * 获取平台信息
     */
    @Override
    protected void initData() {
        super.initData();
        token = PreferencesUtil.getInstance().getString(getActivity(), Constant.TOKEN);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("parentId", "1");
        LKUtil.getHttpManager().postBody(HttpConstant.GET_PLATFROM_URL, map, platformHandler);
    }

    /**
     * 获取币
     * @param platfrom
     */
    private void getCoin(String platfrom) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("platformId", platfrom);
        LKUtil.getHttpManager().postBody(HttpConstant.GET_COIN_BY_PLATFROM_URL, map, coinHandler);
    }

    private Map<String, String> map;

    /**
     * 添加币
     * @param coinBean
     */
    private void addCoin(CoinBean coinBean) {
        rowId = coinBean.getRowId();
        map = new HashMap<>();
        map.put("token", token);
        map.put("coinId", coinBean.getCoinId());
        map.put("coinName", coinBean.getCoinName());
        map.put("platformName", coinBean.getPlatformName());
        map.put("platformId", coinBean.getPlatformId());
        map.put("relativeTrade", coinBean.getRelativeTrade());
        LKUtil.getHttpManager().postBody(HttpConstant.ADDBI_URL, map, addHandler);
    }

    /**
     * 移除币
     * @param coinBean
     */
    private void delCoin(CoinBean coinBean) {
        rowId = coinBean.getRowId();
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("coinId", coinBean.getCoinId());
        map.put("rowId", coinBean.getRowId()+"");
        map.put("platformId", coinBean.getPlatformId());
        map.put("relativeTrade", coinBean.getRelativeTrade());
        LKUtil.getHttpManager().postBody(HttpConstant.DELETE_URL, map, delHandler);
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        //平台点击事件
        platformListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String platformId = ((MapBean) platfromAdapter.getItem(position)).getValue();
                getCoin(platformId);
            }
        });

        //添加移除币点击事件
        coinAdapter.setOnSelfClickListener(new CoinAdapter.ClickListener() {
            @Override
            public void add(CoinBean coinBean) {
                if (coinBean.getFocusFlag()!=null&&coinBean.getFocusFlag().equals("1")){

                    //对话框
                    dialogShow(coinBean);

                }else {

                    //添加币并 存储币的详细信息
                    addCoin(coinBean);

                    PreferencesUtil.getInstance().putString(getContext(), "coinName", coinBean.getCoinName());
                    PreferencesUtil.getInstance().putString(getContext(), "relativeTrade", coinBean.getRelativeTrade());
                    PreferencesUtil.getInstance().putString(getContext(), "platformName", coinBean.getPlatformName());
                    PreferencesUtil.getInstance().putString(getContext(), "coinNameCN", coinBean.getCoinNameCN());
                    PreferencesUtil.getInstance().putString(getContext(), "platformId", coinBean.getPlatformId());
                    PreferencesUtil.getInstance().putString(getContext(), "coinId", coinBean.getCoinId());
                }
            }
        });

        //对话框点击事件
        dialogHint.setOnDialogVersionListener(new DialogHint.OnDialogVersionListener() {

            /**
             * 点击前往预警 跳转预警设置界面
             */
            @Override
            public void onSure() {
                List<ParamUtils.NameValue> parList = ParamUtils.build().put("platformName", map.get("platformName"))
                        .put("coinName", map.get("coinName")).put("coinId", map.get("coinId"))
                        .put("platformId", map.get("platformId")).put("relativeTrade", map.get("relativeTrade"))
                        .create();
                IntentUtils.startAty(getActivity(), SetWarnActivity.class,parList);
                getActivity().finish();
            }

            @Override
            public void onCancel() {

            }
        });
    }

    /**
     * 获取平台列表的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<List<MapBean>> platformHandler = new BaseHttpAsycResponceHandler<List<MapBean>>(true) {
        @Override
        public void onSuccess(List<MapBean> data) {
            super.onSuccess(data);
            if (data != null && !data.isEmpty()) {
                platfromAdapter.updateData(data);
            }
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    /**
     * 获取平台币的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<List<CoinBean>> coinHandler = new BaseHttpAsycResponceHandler<List<CoinBean>>() {

        @Override
        public void onSuccess(List<CoinBean> data) {
            super.onSuccess(data);
            if (data != null && !data.isEmpty()) {
                coinAdapter.updateData(data);
                drawerLayout.openDrawer(coinLayout);

                add_platform.setText(data.get(0).getPlatformName());

                //根据币的platformId 添加图标
                if (data.get(0).getPlatformId().equals("19")){

                    addIcon.setImageResource(R.mipmap.okex);
                }

                if (data.get(0).getPlatformId().equals("3")){

                    addIcon.setImageResource(R.mipmap.huobi);
                }

                if (data.get(0).getPlatformId().equals("5")){

                    addIcon.setImageResource(R.mipmap.bian);
                }

                if (data.get(0).getPlatformId().equals("20")){

                    addIcon.setImageResource(R.mipmap.zb);
                }

                if (data.get(0).getPlatformId().equals("621")){

                    addIcon.setImageResource(R.mipmap.fc);
                }

                if (data.get(0).getPlatformId().equals("24")){

                    addIcon.setImageResource(R.mipmap.bit);
                }

                if (data.get(0).getPlatformId().equals("23")){

                    addIcon.setImageResource(R.mipmap.bibox);
                }

                if (data.get(0).getPlatformId().equals("22")){

                    addIcon.setImageResource(R.mipmap.bidan);
                }

                if (data.get(0).getPlatformId().equals("21")){

                    addIcon.setImageResource(R.mipmap.bter);
                }

            }
            drawerLayout.openDrawer(coinLayout);
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };


    /**
     * 添加币的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> addHandler = new BaseHttpAsycResponceHandler<String>(true) {

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);
            getActivity().setResult(Activity.RESULT_OK);
            dialogHint.setContent("自选成功");
            coinAdapter.updateItemById(rowId,"1");
            dialogHint.show();

        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    /**
     * 移除币的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> delHandler = new BaseHttpAsycResponceHandler<String>(true) {

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);
            getActivity().setResult(Activity.RESULT_OK);
            coinAdapter.updateItemById(rowId,"0");
            Toast.makeText(getActivity(), "移除成功", Toast.LENGTH_SHORT).show();

            //dialog_delHint.show();
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    public void setFlag(boolean isNew) {
        this.isNew = isNew;
    }

    /**
     * 移除币的对话框
     * @param coinBean
     */
    private void dialogShow(final CoinBean coinBean) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View v = inflater.inflate(R.layout.dialog_del, null);

        Button btn_sure = (Button) v.findViewById(R.id.dialog_ok);
        Button btn_cancel = (Button) v.findViewById(R.id.dialog_canle);

        final Dialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);

        //点击确认执行移除币的方法
        btn_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                delCoin(coinBean);
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
    }

}
